#include "DataANO_DP.h"


#if ANODP_LOG_EN

unsigned char ano_scope;
unsigned char ano_count;

/*---------------------------------------------------------------------------*/
/*  输出至 匿名上位机协议
 *  开源地址: https://gitee.com/anotc/AnoAssistant
 *  注意输出的长度为 (HEAD: 1byte,S_ADDR: 1byte,D_ADDR: 1byte,ID: 1byte,LEN: 2byte)6 + (LEN)data + 2,要保证数组大于这个值
 *
 *---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/
/* Name     :   ano_sunadd_16
 * Input    :
 * Output   :
 * Description: 匿名上位机的校验方式
 *---------------------------------------------------------------------------*/
unsigned short int ano_sunadd_16(const unsigned char *input_str, unsigned short int num_bytes)
{
    unsigned short int sumcheck = 0;
    unsigned short int addcheck = 0;
    unsigned short int check_value = 0;
    const unsigned char *ptr;
    unsigned short int a;

    ptr = input_str;
    for (a = 0; a < num_bytes; a++)
    {
        sumcheck += ptr[a];
        addcheck += sumcheck;
    }

    check_value = (sumcheck << 8) + (addcheck & 0x00FF);

    return check_value;
}

/*---------------------------------------------------------------------------*/
/* Name     :   ano_get_channel_data
 * Input    :   value:  通道数据
 *              *output_buffer: 待发送的数据指针
 *              data_type: 通道类型 uint8/int8: 1  uint16/int16: 2
 *              channel_number: 通道数
 * Output   :
 * Description:
 *---------------------------------------------------------------------------*/
void ano_get_channel_data(float value, unsigned char *output_buffer, unsigned char data_type, unsigned char channel_number)
{
    static unsigned short int data_len = 6;
    unsigned long value_temp = 0;
    unsigned char *point;
    point = (unsigned char*)(&value);	  //得到float的地址

    if (channel_number == 1)    data_len = 6;

    value_temp = (unsigned long)value;
    switch (data_type)
    {
    case 1:
        output_buffer[data_len] = (unsigned char)(value_temp & 0xff);
        data_len += 1;
        break;
    case 2:
        output_buffer[data_len] = (unsigned char)(value_temp & 0xff);
        output_buffer[data_len+1] = (unsigned char)((value_temp >> 8) & 0xff);
        data_len += 2;
        break;

    default:
        output_buffer[6+data_len] = (unsigned char)(value_temp & 0xff);
        data_len += 1;
        break;
    }

    output_buffer[4] = (unsigned char)((data_len - 6) & 0xff);
    output_buffer[5] = (unsigned char)(((data_len - 6)>>8) & 0xff);
}

/*---------------------------------------------------------------------------*/
/* Name     :   ano_data_generate
 * Input    :
 *              data_id: 功能码 0xF1-0xFA
 *              *output_buffer: 待发送的数据指针
 * Output   :
 * Description: 输出设备信息
 *---------------------------------------------------------------------------*/
unsigned char ano_data_generate(unsigned char data_id, unsigned char *output_buffer)
{
    unsigned char len = 0;
    unsigned short int temp_l = 0;
    unsigned short int temp_h = 0;
    unsigned short int check_temp;

    output_buffer[0] = 0xAB;
    output_buffer[1] = 0x01;
    output_buffer[2] = 0x01;
    output_buffer[3] = data_id;

    temp_l = ((unsigned short int)output_buffer[4]) & 0xff;
    temp_h = ((unsigned short int)output_buffer[5]) & 0xff;
    len = (unsigned char)((temp_h << 8) + temp_l);

    check_temp = ano_sunadd_16(output_buffer, len+6);
    output_buffer[len+6] = (unsigned char)((check_temp >> 8) & 0xff);
    output_buffer[len+6+1] = (unsigned char)(check_temp & 0xff);

    return len+6+2;

}


#endif

